﻿using Newtonsoft.Json.Linq;
using Pay.Core;
using Pay.DTO;
using Pay.IService;
using Pay.TO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using Flurl;
using Flurl.Util;
using Flurl.Http;

namespace Pay.Token.Controllers {
    [RoutePrefix("task")]
    public class SoupTaskController : BaseController {
        private ISoupTaskService _iSoupTaskService;
        private IMerchantService _iMerchantService;
        public SoupTaskController(ISoupTaskService iSoupTaskService, IMerchantService iMerchantService) {
            _iSoupTaskService = iSoupTaskService;
            _iMerchantService = iMerchantService;
        }

        /// <summary>
        /// 获得任务列表
        /// </summary>
        /// <returns></returns>
        [Route("get_tasks"), HttpGet, AllowAnonymous]
        public ResultMessage<List<task_dto>> GetSoupTaskList() {
            var result = _iSoupTaskService.GetTaskList(UserId);
            return ResultToJson.Json(result);
        }
        /// <summary>
        /// 获得任务列表 version 2.0
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [Route("get_tasks_v2"), HttpPost, AllowAnonymous]
        public ResultMessage<List<task_dto>> GetAppStoreSoupTaskList(task_query query) {
            query.user_id = UserId;
            var result = _iSoupTaskService.GetTaskList(query);
            return ResultToJson.Json(result);
        }
        /// <summary>
        /// 获得任务详情（记录点击数）
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [Route("get_task_detail"), HttpPost]
        public ResultMessage<task_dto> GetSoupTask([FromBody]soup_task_query query) {
            var result = _iSoupTaskService.AppGetSoupTask(query.id, UserId);
            return ResultToJson.Json(result);
        }

        /// <summary>
        /// 阅读蚂蚁领取马力
        /// </summary>
        /// <returns></returns>
        [Route("task_mayi"), HttpPost]
        public async Task<ResultMessage<bool>> TaskMayi() {
            var result = await _iSoupTaskService.TaskRecive(UserId, ETaskType.MaYiBuLuo);
            return ResultToJson.Json(result);
        }
        /// <summary>
        /// 蚂蚁快讯
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [Route("get_news"), HttpGet, AllowAnonymous, Anonymous]
        public async Task<ResultMessage<PagedResult<news_flash_dto>>> GetNews([FromUri]PageQuery query) {

            try {
                string url = t_RequestMethod.mayiapi + "/channel/get_newsflashs" + "?page=" + query.page + "&size=" + query.size;
                var result = await url.GetJsonAsync<ResultMessage<PagedResult<news_flash_dto>>>();
                if (result.result_code == 1000) {
                    return ResultToJson.Json(result.data);
                } else return ResultToJson.Json(new PagedResult<news_flash_dto>());
            } catch (FlurlHttpException ex) {
                return ResultToJson.Json(new PagedResult<news_flash_dto>());
            }

        }
        /// <summary>
        /// 阅读蚂蚁领取马力(web) app 不用调用
        /// </summary>
        /// <returns></returns>
        [Route("task_mayi_web"), HttpPost, AllowAnonymous, Anonymous]
        public async Task<ResultMessage<bool>> TaskMayiWeb([FromBody]JObject merchant_no) {
            var m = GetJsonValue<string>("merchant_no", merchant_no);
            var member = _iMerchantService.Get(m);
            if (member == null) throw new ServerErrorException("用户不存在");
            var result = await _iSoupTaskService.TaskRecive(member.id, ETaskType.MaYiBuLuo);
            return ResultToJson.Json(result);
        }
        /// <summary>
        /// 关注公众号领取马力
        /// </summary>
        /// <returns></returns>
        [Route("task_code"), HttpPost]
        public async Task<ResultMessage<bool>> TaskCode([FromBody]JObject code) {
            var jcode = GetJsonValue<string>("code", code);
            var result = await _iSoupTaskService.TaskRecive(UserId, ETaskType.FollowPubAccount, 0, jcode);
            return ResultToJson.Json(result);
        }
        /// <summary>
        /// 时代云商领取马力
        /// </summary>
        /// <returns></returns>
        [Route("task_shidai"), HttpPost]
        public async Task<ResultMessage<bool>> Taskshidai() {
            var result = await _iSoupTaskService.TaskRecive(UserId, ETaskType.ShiDaiYunShang);
            return ResultToJson.Json(result);
        }
        /// <summary>
        /// 币金所领取马力
        /// </summary>
        /// <returns></returns>
        [Route("task_coin"), HttpPost]
        public async Task<ResultMessage<bool>> TaskBijin() {
            var result = await _iSoupTaskService.TaskRecive(UserId, ETaskType.CoinGold);
            return ResultToJson.Json(result);
        }
        /// <summary>
        /// 哥伦布领取马力
        /// </summary>
        /// <returns></returns>
        [Route("task_gelunbu"), HttpPost]
        public async Task<ResultMessage<bool>> TaskGelunbu() {
            var result = await _iSoupTaskService.TaskRecive(UserId, ETaskType.GeLunBu);
            return ResultToJson.Json(result);
        }

        /// <summary>
        /// 观看直播领取马力
        /// </summary>
        /// <returns></returns>
        [Route("task_watchlive"), HttpPost]
        public async Task<ResultMessage<bool>> TaskWatchLive() {
            var result = await _iSoupTaskService.TaskRecive(UserId, ETaskType.WatchLive);
            return ResultToJson.Json(result);
        }

        /// <summary>
        /// 浏览商城领取马力
        /// </summary>
        /// <returns></returns>
        [Route("task_viewmall"), HttpPost]
        public async Task<ResultMessage<bool>> TaskViewMall() {
            var result = await _iSoupTaskService.TaskRecive(UserId, ETaskType.ViewMall);
            return ResultToJson.Json(result);
        }

        /// <summary>
        /// 实名任务
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [Route("task_auth"), HttpPost]
        public async Task<ResultMessage<authinfo_req>> TaskAuth([FromBody]token_auth_model model) {
            var result = await _iMerchantService.VerificationCard(model, UserId);
            if (result == null)
                return ResultToJson.Json<authinfo_req>("实名提交失败");
            return ResultToJson.Json(result);
        }
    }
}

